package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Message;
import cn.wolfcode.trip.base.domain.MessageContent;
import cn.wolfcode.trip.base.query.friend.MessageQuery;
import cn.wolfcode.trip.base.service.IMessageService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("messages")
public class MessageController {
    @Autowired
    private IMessageService messageService;

    @PostMapping
    public JSONResult sendMassage(Message message){
        JSONResult result = new JSONResult();
        try {
            messageService.sendMassage(message);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @GetMapping("/invites")
    public Object getInviteList(Long id){
        Map<String,Object> map = messageService.getInviteList(id);
        return map;
    }


    @GetMapping("/{id}")
    public Object getMessage(@PathVariable Long id){
        Map<String,Object> map = messageService.getOneMessage(id);
        return map;
    }

    @PutMapping("/markread")
    public JSONResult markRead(Long messageId){
        JSONResult result = new JSONResult();
        try {
            messageService.markRead(messageId);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @PutMapping("/batchMarkread")
    public JSONResult batchMarkread(Long userId,Long targetId,Integer type){
        JSONResult result = new JSONResult();
        try {
            messageService.batchMarkread(userId,targetId,type);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @GetMapping("/dialogClue/{id}")
    public Object getMessageDialogClue(@PathVariable Long id){
        List<Map<String, Object>> dialogClue = messageService.getMessageDialogClue(id);
        return dialogClue;
    }

    @GetMapping("/dialogs")
    public Object getMessageDialogs(Long userId,Long targetId,Integer type){
        List<Message> list = messageService.getMessageDialog(userId,targetId,type);
        return list;
    }

    @PostMapping("/dialogs")
    public JSONResult sendMessageDialog(Message message){
        JSONResult result = new JSONResult();
        try {
            message.setType(Message.TYPE_NORMAL);
            messageService.sendMassage(message);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    @GetMapping("/crowds/{crowdId}/{userId}")
    public Object getInviteCrowdMessage(@PathVariable Long crowdId,@PathVariable Long userId){
        List<MessageContent> list = messageService.getInviteCrowdMessage(crowdId,userId);
        return list;
    }

    @GetMapping("/crowds/dialog")
    public Object getMessageCrowdDialog(Long userId,Long targetId,Integer type){
        List<Message> list = messageService.getMessageCrowdDialog(userId,targetId,type);
        return list;
    }


    //获取群会话列表
    @GetMapping("/crowdDialogClue/{id}")
    public Object getMessageCrowdDialogClue(@PathVariable Long id){
        List<Map<String, Object>> dialogClue = messageService.getMessageCrowdDialogClue(id);
        return dialogClue;
    }

    @GetMapping("/addFriendMessage")
    public Object getAddFriendMessage(Long userId,Long targetId){
        Message message = messageService.getAddFriendMessage(userId,targetId);
        return message;
    }
}
